<?php

class Reservation extends CI_Controller {

    function __construct() {
        parent::__construct();
        $level_id = $this->session->userdata('logged_in');
        if ($level_id['level_id'] == null) {
            redirect('user/login', 'refresh');
        }
        if ($level_id['level_id'] == 0) {
            redirect('user', 'refresh');
        }
    }

    function index() {
        $crud = new grocery_CRUD();
        $crud->set_table('reservation');
		$crud->order_by('resv_id', 'desc');
        $crud->set_subject('Reservation');
        $crud->set_relation('user_id', 'user', 'emp_name');
        $crud->set_relation('guest_id', 'guest', 'guest_name');
        $crud->columns('guest_id', 'room_number', 'resv_adult_number', 'resv_child_number', 'resv_infant_number', 'resv_type','user_id','_');
        $crud->display_as('user_id', 'Staff Name');
        $crud->display_as('guest_id', 'Guest Name');
        $crud->display_as('resv_adult_number', 'Number of Adult');
        $crud->display_as('resv_child_number', 'Number of Child');
        $crud->display_as('resv_infant_number', 'Number of Infant');
        $crud->display_as('resv_type', 'Reservation Type');
        
		$crud->callback_column('_', array($this, 'EditResv'));
		$crud->callback_column('room_number', array($this, 'guest_room'));
		//$crud->add_action('_','','', 'icon-th-list',array());
		$crud->unset_add();
    	$crud->unset_edit();
	    $crud->unset_read();
    	$crud->unset_delete();
		$output = (array) $crud->render();
        $output['title'] = 'Reservation';
		$this->load->template('reservation/index', $output);
    }
	
	function guest_room($value, $row) {
		$this->load->model('reservation_model');
		$resv_rooms = $this->reservation_model->get_resv_room($row->resv_id)->result_array();
		$room_arr = array();
		foreach($resv_rooms as $room){
			$room_arr[] = $room['room_number'];
		}
		$room_arr = array_unique($room_arr);
		natsort($room_arr);

		return implode(' | ', $room_arr);
	}

    public function editResv($value, $row) {
				
		return 
            '<a style="color:white" class="btn btn-info btn-mini" href="' . base_url() . 'reservation/editRoom/' . $row->resv_id . '">Room</a>
            <a style="color:white" class="btn btn-info btn-mini" href="' . base_url() . 'reservation/service/' . $row->resv_id . '">Service</a>
            <a style="color:white" class="btn btn-success btn-mini" href="' . base_url() . 'reservation/pay/' . $row->resv_id . '">Payment</a>';
			
		//detail isikan disini nanti an//
		
    }
	
	function addResv() {
        $data['css_files'] = false;
        $data['title'] = 'Add Reservation';

        $this->load->template('reservation/add', $data);
    }

    function guestSearch() {
        $crud = new grocery_CRUD();

        $crud->set_table('guest');
        $crud->set_subject('Guest');
		$crud->where('deleted','No');
        $crud->required_fields('guest_name', 'guest_address', 'guest_phone', 'guest_group');
        $crud->columns('guest_name', 'guest_ktp', 'guest_address', 'guest_phone', 'guest_type', 'nationality', 'Choose Guest');
        $crud->order_by('guest_id', 'desc');

        $crud->display_as('guest_name', 'Name');
        $crud->display_as('guest_ktp', 'Identity Card');
        $crud->display_as('guest_address', 'Address');
        $crud->display_as('guest_phone', 'Phone Number');
        $crud->display_as('guest_type', 'Guest Type');

        $crud->callback_field('guest_ktp', array($this, 'KTP_desc'));
        $crud->callback_column('Choose Guest', array($this, 'chooseGuest'));
		
		$crud->add_fields('guest_name', 'pic_agency', 'guest_ktp', 'guest_address', 'guest_phone', 'nationality', 'guest_type', 'guest_desc');
		$crud->edit_fields('guest_name', 'pic_agency', 'guest_ktp', 'guest_address', 'guest_phone', 'nationality', 'guest_type', 'guest_desc');

        $crud->unset_edit();
        $crud->unset_read();
        $crud->unset_delete();
        $crud->unset_export();
        $crud->unset_print();

        $output = $crud->render();
        $this->load->view('reservation/guestsearch', $output);
    }

    function KTP_desc($value = '') {
        return '<input type="number" maxlength="16" value="' . $value . '" name="guest_ktp" style="width:auto"> (Fill if personal)';
    }

    public function chooseGuest($value, $row) {
        return '<a class="btn btn-primary" href="javascript:void(0)" onclick="choose(\'' . $row->guest_id . '\', \'' . $row->guest_name . '\')">Choose</a>';
    }
	
	
    function roomSearchAjax() {
        $this->load->model('reservation_model');

        if (!$_POST['checkin'])
            $_POST['checkin'] = 0;
        if (!$_POST['checkout'])
            $_POST['checkout'] = 0;
        if (!$_POST['adult'])
            $_POST['adult'] = 0;
        if (!$_POST['child'])
            $_POST['child'] = 0;
        if (!$_POST['infant'])
            $_POST['infant'] = 0;

        $data = $this->reservation_model->roomSearch($_POST['checkin'], $_POST['checkout'], $_POST['adult'], $_POST['child'], $_POST['infant']);

        echo json_encode($data);
    }
	
	function roomSearch_model() {
        $this->load->model('reservation_model');
		//var_dump($_POST);
        $data['room'] = $this->reservation_model->roomSearch($_POST['date']);
		$data['rate'] = $this->reservation_model->select_rate_type($_POST['date']);
        echo json_encode($data);
    }
	
	function choose_room_model() {
        $this->load->model('reservation_model');
		//var_dump($_POST);
		date_default_timezone_set('Asia/Jakarta');
		$now = date('Y-m-d H:m:s');
		$data['now'] = $now;
		//var_dump($data);
        $id = $this->reservation_model->insert_Room($_POST['room_id'], $_POST['date']);
		$data['room'] = $this->reservation_model->select_resv_room($id);
		$data['rate'] = $this->reservation_model->select_rate_type($_POST['room_id'], $_POST['date']);
        echo json_encode($data);
    }
	
	function select_rate(){
		$this->load->model('reservation_model');
		$data = $this->reservation_model->select_rate_price($_POST['rate_id'], $_POST['room_type_id'], $_POST['date']);
		echo json_encode($data);
	}
	
	function orderedRoom(){
		$this->load->model('reservation_model');
		//var_dump($_POST);
		$date = date("Y-m-d H:i:s");
		if($_POST['status']=="Booked"){
			if($_POST['rate_id']){
				$resv_room = $this->reservation_model->insert_resv_rate_room1($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['rate_price'], $date, $_POST['rate_id']);
				$data = $this->reservation_model->resv_room_id($resv_room);
				//var_dump($data);
				echo json_encode($data);
			} else{
				$resv_room = $this->reservation_model->insert_resv_room1($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['price'], $date);
				$data = $this->reservation_model->resv_room_id($resv_room);
				//var_dump($data);
				echo json_encode($data);
		}
		} else {
			if($_POST['status']=="Check In"){
				if($_POST['rate_id']){
				$resv_room = $this->reservation_model->insert_resv_rate_room2($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['rate_price'], $date, $_POST['rate_id']);
				$data = $this->reservation_model->resv_room_id($resv_room);
				//var_dump($data);
				echo json_encode($data);
				} else{
				$resv_room = $this->reservation_model->insert_resv_room2($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['price'], $date);
				$data = $this->reservation_model->resv_room_id($resv_room);
				//var_dump($data);
				echo json_encode($data);
				}
			}
		}
	}
	
	function orderNewRoom(){
		$this->load->model('reservation_model');
		//var_dump($_POST);
		$date = date("Y-m-d H:i:s");
		if($_POST['status']=="Booked"){
        $resv_room = $this->reservation_model->insert_new_room1($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['price'], $date, $_POST['resv_id']);
		$data = $this->reservation_model->resv_new_room_id($resv_room);
		//var_dump($data);
        echo json_encode($data);
		} else {
			if($_POST['status']=="Check In"){
				$resv_room = $this->reservation_model->insert_new_room2($_POST['tanggal'], $_POST['status'], $_POST['room_id'], $_POST['price'], $date, $_POST['resv_id']);
				$data = $this->reservation_model->resv_new_room_id($resv_room);
				//var_dump($data);
				echo json_encode($data);
			}
		}
	}

    function roomPriceAjax() {
        $this->load->model('reservation_model');

        $data = $this->reservation_model->roomPrice($_POST['checkin'], $_POST['checkout'], $_POST['room_number']);

        echo json_encode($data);
    }
	
	function save(){
		$session = $this->session->userdata('logged_in');
        $this->load->model('reservation_model');
		$guest_id = $this->input->post('guest_id');
		$adult = $this->input->post('adult');
		$child = $this->input->post('child');
		$infant = $this->input->post('infant');
		$resv_type = $this->input->post('resv_type');
		$status = $this->input->post('statusord');
		$rate = $this->input->post('rate');
		$rate_price = $this->input->post('rate_price2');
		$resv_room_id = $this->input->post('resv_room_id');
		date_default_timezone_set('Asia/Jakarta');
		$time = date('Y-m-d H:i:s');
		//var_dump($time);
		if($guest_id[0]){
				$resv_id = $this->reservation_model->saveResv($session['user_id'], $guest_id, $adult, $child, $infant, $resv_type);
			for($i = 0; $i< count($status); $i++ ){
				//echo 'asdasd';
				if($status[$i] == 'Booked'){
				
				//echo $status[$i];
				//echo $resv_id;
				//echo $resv_room_id[$i];
				//echo $rate[$i];
				//echo $rate_price[$i];
					if($rate[$i]){
						$this->reservation_model->update_resv_room_id_RB($resv_id, $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
					} else {
						$this->reservation_model->update_resv_room_id_B($resv_id, $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
						}
					
				} else {
					if($status[$i] =='Check In'){
						if($rate){
							$this->reservation_model->update_resv_room_id_RCI($resv_id, $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
						}else{
							$this->reservation_model->update_resv_room_id_CI($resv_id, $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
							}
					}else{
						if ($status[$i] =='Out Of Order'){
							if($rate[$i]){
								$this->reservation_model->update_resv_room_id_OOR($resv_id, $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
							}else{
								$this->reservation_model->update_resv_room_id_OO($resv_id, $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
								}
						} else {
						
						}
					
						}
				
				}
			}
			redirect('reservation/index');
		} else {
			echo "guest belum diisi";
			echo "<br />";
			echo "<a class ='btn btn-primary' href='".base_url('reservation/addResv')."'>Back To Reservation</a>";
			
		}
	}
    /*function save(){
		$session = $this->session->userdata('logged_in');
        $this->load->model('reservation_model');
		//var_dump($_POST['i']);
		if($_POST['guest_id']){
		$resv_id = $this->reservation_model->saveResv($session['user_id']);
		$this->reservation_model->update_resv_room_id($resv_id, $_POST['resv_id']);
		//var_dump($a);
		//redirect('reservation');
		} else {
			echo"guest belum diisi";
		}
    }*/
	
	function update_resv(){
			$resv_id = $this->reservation_model->last_id();
			$this->reservation_model->update_resv_room_id($resv_id, $_POST['resv_id']);
	}
	
	function cancelRoom(){
		$this->load->model('reservation_model');
		$this->reservation_model->cancel_room($_POST['resv_id']);
	}
	
	function cancelRoomOrdered($resv_id, $resv_room_id){
		$this->load->model('reservation_model');
		$this->reservation_model->cancel_room_ordered($resv_room_id);
		redirect('reservation/editRoom/'.$resv_id);
	}
	
	function checkinRoom($resv_id, $resv_room_id){
		date_default_timezone_set('Asia/Jakarta');
		$time = date('Y-m-d H:i:s');
		$this->load->model('reservation_model');
		$this->reservation_model->checkin_room($resv_room_id, $time);
		redirect('reservation/editRoom/'.$resv_id);
	}
	
	function checkoutRoom($resv_id, $resv_room_id, $room_id){
		date_default_timezone_set('Asia/Jakarta');
		$time = date('Y-m-d H:i:s');
		$this->load->model('room_model');
		$this->load->model('reservation_model');
		$this->reservation_model->checkout_room($resv_room_id, $time);
		$this->room_model->dirty($room_id);
		redirect('reservation/editRoom/'.$resv_id);
	}

    function edit($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Edit Reservation';

        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->getResv($resv_id);

        $this->load->template('reservation/edit', $output);
    }

    function editSave() {
        $this->load->model('reservation_model');	
	
		if($_POST['date_change']>0){
		$this->reservation_model->extendCheckOut();
		}
        $this->reservation_model->editResv();
    }

    function editRoom($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Room List';

        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->editRoom($resv_id);
		$this->load->model('service_model');
		$output['is_cancelable'] = $this->service_model->is_cancelable($resv_id);
		$output['resv_id'] = $resv_id;

        $this->load->template('reservation/resvroom', $output);
    }

    function service($resv_id) {
        $data['css_files'] = false;
        $data['title'] = 'Service List';
        $data['resv_id'] = $resv_id;
        
        $this->load->model('reservation_model');
		
        $resv = $this->reservation_model->listServ($resv_id)->result_array();
		
		$order = array();
		foreach($resv as $row) {
			$row['serv_det'] = $this->reservation_model->serv_det($row['serv_ord_id'])->result_array();
			$order[] = $row;
		}
		
		$data['order'] = $order;
		$this->load->model('service_model');
		$data['is_cancelable'] = $this->service_model->is_cancelable($resv_id);
		

        $this->load->template('reservation/listService', $data);
    }

    function addServ($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Add Service';
        $output['resv_id'] = $resv_id;
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->listServ($resv_id);

        $this->load->template('reservation/addServ', $output);
    }
    
    function saveServ(){
        $this->load->model('reservation_model');
        $this->reservation_model->addServ();
    }
    
    function editServ($id) {
        $output['css_files'] = false;
        $output['title'] = 'Edit Service List';
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->getServ($id);
$output['data2'] = $this->reservation_model->listServ();
        $this->load->template('reservation/editServ', $output);
    }
    
    function saveEditServ(){
        $this->load->model('reservation_model');
        $this->reservation_model->editServ();
    }
    
    function delServ($id) {
        $this->load->model('reservation_model');
        $this->reservation_model->deleteServ($id);
        redirect('services','refresh');
    }
	
	function room_service($resv_id, $resv_room_id) {
		$this->load->model('reservation_model');
		$crud = new grocery_CRUD();
		$crud->set_subject('Room Service');
		$crud->set_table('reservation_material');
		$crud->set_relation('material_id', 'material', 'material_name');				
		$crud->where('reservation_material.resv_room_id', $resv_room_id);
		$crud->where('reservation_material.deleted', 'No');
		$crud->order_by('resv_mat_time', 'desc');
		
		$crud->columns('resv_mat_time', 'material_id', 'resv_mat_price', 'resv_mat_qty', 'resv_mat_desc');
		$crud->callback_column('resv_mat_price', array($this, 'formatRupiah'));
		$crud->callback_column('resv_mat_qty', array($this, 'point_to_comma'));
		
		$crud->display_as('resv_mat_time', 'Time');
		$crud->display_as('material_id', 'Room Service');
		$crud->display_as('resv_mat_price', 'Price');
		$crud->display_as('resv_mat_qty', 'Qty');
		$crud->display_as('resv_mat_desc', 'Description');
		
		
		$crud->fields('resv_mat_time', 'material_id', 'resv_mat_qty', 'resv_mat_desc');
		$crud->required_fields('resv_mat_time', 'material_id', 'resv_mat_qty');
		
		$crud->callback_field('resv_mat_qty', array(
			$this,
			'cb_resv_mat_qty'
		));
		$crud->callback_insert(array(
			$this,
			'cb_insert_room_service'
		));
		$crud->callback_update(array(
			$this,
			'cb_update_room_service'
		));
		$crud->callback_delete(array(
			$this,
			'cb_delete_room_service'
		));
		
		if($this->reservation_model->is_checked_out($resv_room_id)) {
			$crud->unset_operations();
		}
		
		$sess = $this->session->userdata('logged_in');
		if($sess['level_id'] == 3) {
			$crud->unset_delete();
			$crud->unset_edit();
		}
		
		$output          = (array) $crud->render();
		$state = $crud->getState();
		if($state == 'list' || $state == 'success') $output['show_button'] = true;
		else $output['show_button'] = false;
		$output['title'] = 'Room Service';
		$output['resv_id'] = $resv_id;
		$output['resv_room_id'] = $resv_room_id;
		
		$this->load->template('reservation/room_service', $output);
	}
	
	function cb_resv_mat_qty($value, $row) {
		return '<input class="qty" type="text" name="resv_mat_qty" value="'.$value.'" style="width:100px;"><input type="hidden" name="resv_room_id">';
	}
	
	function cb_insert_room_service($post) {
		$this->load->model('reservation_model');
		$mat = $this->reservation_model->select_material($post['material_id'])->row_array();
		$sess = $this->session->userdata('logged_in');
		$post['last_modifier'] = $sess['user_id'];
		$post['resv_mat_price'] = $mat['material_rent_price'];
		$post['resv_mat_qty'] = $this->load->comma_to_point($post['resv_mat_qty']);
		$post['resv_mat_time'] = $this->load->std_time($post['resv_mat_time'], '/');
		$this->reservation_model->insert_reservation_material($post);
	}
	
	function cb_update_room_service($post, $pk) {
		$this->load->model('reservation_model');
		$mat = $this->reservation_model->select_material($post['material_id'])->row_array();
		$sess = $this->session->userdata('logged_in');
		$post['last_modifier'] = $sess['user_id'];
		$post['resv_mat_price'] = $mat['material_rent_price'];
		$post['resv_mat_qty'] = $this->load->comma_to_point($post['resv_mat_qty']);
		$post['resv_mat_time'] = $this->load->std_time($post['resv_mat_time'], '/');
		$this->reservation_model->update_reservation_material($pk, $post);
	}
	
	function cb_delete_room_service($pk) {
		$this->load->model('reservation_model');
		$sess = $this->session->userdata('logged_in');
		$data['last_modifier'] = $sess['user_id'];
		$data['deleted'] = 'yes';
		$this->reservation_model->update_reservation_material($pk, $data);
	}

    function pay($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Payment';
        $output['resv_id'] = $resv_id;
        
        $_SESSION['loc'] = $_SERVER['PHP_SELF'];
        
        $this->load->model('reservation_model');
        $this->load->model('service_model');
        $output['service'] = $this->reservation_model->get_service_cost($resv_id)->row_array();
        $output['room'] = $this->reservation_model->get_room_cost($resv_id)->row_array();
		$output['material'] =$this->reservation_model->get_material_cost($resv_id)->row_array();
		$output['resv'] =$this->reservation_model->get_resv($resv_id)->row_array();
		$output['payment'] =$this->reservation_model->get_payment($resv_id)->result_array();
		$output['is_cancelable'] = $this->service_model->is_cancelable($resv_id);
		
        $this->load->template('reservation/payment', $output);
    }
	
	function edit_pay() {
		$resv_id =  $this->input->post('resv_id');
		$paym = array(
			'ppn_tax' => $this->input->post('ppn'),
			'service_tax' => $this->input->post('service'),
			'resv_desc' => $this->input->post('desc'),
			'last_modifier' => $sess['user_id']
		);
		if($this->input->post('charge') == 'percent') {
			$paym['extra_charge_percent'] = $this->input->post('charge_percent');
			$paym['extra_charge_nominal'] = null;
		}
		else if($this->input->post('charge') == 'nominal') {
			$paym['extra_charge_nominal'] = $this->input->post('charge_nominal');
			$paym['extra_charge_percent'] = null;
		}
		else {
			$paym['extra_charge_nominal'] = null;
			$paym['extra_charge_percent'] = null;
		}
		
		if($this->input->post('disc') == 'percent') {
			$paym['discount_percent'] = $this->input->post('disc_percent');
			$paym['discount_nominal'] = null;
		}
		else if($this->input->post('disc') == 'nominal') {
			$paym['discount_nominal'] = $this->input->post('disc_nominal');
			$paym['discount_percent'] = null;
		}
		else {
			$paym['discount_nominal'] = null;
			$paym['discount_percent'] = null;
		}
		$this->load->model('reservation_model');
		$this->reservation_model->edit_resv($paym, $resv_id);
	}
    
    function addPayment($resv_id, $balance){
        $output['css_files'] = false;
        $output['title'] = 'Add Payment';
        $output['resv_id'] = $resv_id;
        
        $this->load->model('reservation_model');
        $output['payment_type'] = $this->reservation_model->listPayment()->result_array();
		//$output['data2'] = $this->reservation_model->getBiaya($resv_id);
		$output['balance'] = $balance;

        $this->load->template('reservation/addPaym', $output);
    }
    
    function savePaym(){
        $this->load->model('reservation_model');
		$sess = $this->session->userdata('logged_in');
		$paym = array(
			'resv_id' => $this->input->post('resv_id'),
			'payment_id' => $this->input->post('payment_id'),
			'resv_paym_total' => $this->input->post('total'),
			'special_code' => $this->input->post('code'),
			'resv_paym_desc' => $this->input->post('desc'),
			'last_modifier' => $sess['user_id']
		);
		
        $this->reservation_model->addPayment($paym);
    }
    
    function editPaymentPage($id){
        $output['css_files'] = false;
        $output['title'] = 'Edit Service List';
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->getEditPaym($id);
$output['data2'] = $this->reservation_model->listPayment();
        $this->load->template('reservation/editPaym', $output);
        
    }
    
    function editPayment(){
         $this->load->model('reservation_model');
        $this->reservation_model->edit();
    }
    
    function delPayment($id){
        $this->load->model('reservation_model');
        $this->reservation_model->delPayment($id);
        header('Location: '.$_SERVER['HTTP_REFERER']);
    }

    function payment_type() {
        $crud = new grocery_CRUD();
        $crud->set_table('payment_type');
        $crud->set_subject('Payment Type');
		$crud->where('deleted','No');
        $crud->required_fields('payment_code', 'payment_name');
        $crud->columns('payment_code', 'payment_name', 'payment_desc');
        $crud->display_as('payment_code', 'Payment Code');
        $crud->display_as('payment_name', 'Payment Type');
        $crud->display_as('payment_desc', 'Description');
		
		$crud->unset_edit_fields('payment_id', 'last_modifier', 'last_modified', 'deleted');
		$crud->unset_add_fields('payment_id', 'last_modifier', 'last_modified', 'deleted');
		
		$crud->callback_add_field('payment_code',array($this,'get_max_code'));
		$crud->callback_after_update(array($this, 'after_edit_payment'));
		$crud->callback_delete(array($this, 'after_delete_payment'));
		$crud->callback_after_insert(array($this, 'after_insert_payment'));

        $output = (array) $crud->render();
        $output['title'] = 'Payment Type List';

        $this->load->template('reservation/paymenttype', $output);
    }
	
	function after_edit_payment($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('payment_type', array('last_modifier'=>$level_id['user_id']),array('payment_id'=>$primary_key));
	}
	
	function after_delete_payment($primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('payment_type', array('deleted'=>'Yes', 'last_modifier'=>$level_id['user_id']),array('payment_id'=>$primary_key));
	}
	
	function after_insert_payment($post, $primary_key){
		$level_id = $this->session->userdata('logged_in');
		$this->db->update('payment_type', array('last_modifier'=>$level_id['user_id']),array('payment_id'=>$primary_key) );
	}
	
	function get_max_code(){
		$query = "select max(payment_code) as max from payment_type";
		$result = $this->db->query($query)->row();
		$string = (int) $result->max+1;
		return '<input type="text" maxlength="4" value=" '.$string.' " name="payment_code" style="width:auto">';
	}
	
	function formatRupiah($value) {
		if($value != null) {
			return 'Rp  ' . number_format($value, 2, ',', '.');
		}
		else {
			return $value;
		}
    }
	
	function point_to_comma($number) {
		return str_replace('.',',',$number);
	}
	
	function choose_room() {
		$data['css_files'] = false;
		$data['title']     = 'Choose Room';
		
		$this->load->view('reservation/choose_room', $data);
	}
	
	function addRoom(){
		$session = $this->session->userdata('logged_in');
        $this->load->model('reservation_model');
		$guest_id = $this->input->post('guest_id');
		$adult = $this->input->post('adult');
		$child = $this->input->post('child');
		$infant = $this->input->post('infant');
		$resv_type = $this->input->post('resv_type');
		$status = $this->input->post('statusord');
		$rate = $this->input->post('rate');
		$rate_price = $this->input->post('rate_price2');
		$resv_room_id = $this->input->post('new_resv_room_id');
		date_default_timezone_set('Asia/Jakarta');
		$time = date('Y-m-d H:i:s');
		$resv_id = $this->input->post('resv_id');
		//var_dump($time);
		
			for($i = 0; $i< count($status); $i++ ){
				//echo 'asdasd';
				if($status[$i] == 'Booked'){
				
				//echo $status[$i];
				//echo $resv_id;
				//echo $resv_room_id[$i];
				//echo $rate[$i];
				//echo $rate_price[$i];
					if($rate[$i]){
						$this->reservation_model->update_resv_room_id_RB($resv_id[0], $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
					} else {
						$this->reservation_model->update_resv_room_id_B($resv_id[0], $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
						}
					
				} else {
					if($status[$i] =='Check In'){
						if($rate){
							$this->reservation_model->update_resv_room_id_RCI($resv_id[0], $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
						}else{
							$this->reservation_model->update_resv_room_id_CI($resv_id[0], $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
							}
					}else{
						if ($status[$i] =='Out Of Order'){
							if($rate[$i]){
								$this->reservation_model->update_resv_room_id_OOR($resv_id[0], $resv_room_id[$i], $status[$i], $rate[$i], $rate_price[$i], $time);
							}else{
								$this->reservation_model->update_resv_room_id_OO($resv_id[0], $resv_room_id[$i], $status[$i],$rate_price[$i], $time);
								}
						} else {
						
						}
					
						}
				
				}
			}
			redirect('reservation/editRoom/'.$resv_id[0]);
		
	}
	
	
			
		
	

}